local ActDefine = require("activity.Define")
--local Util = require("common.Util")
local ActivityExcel = require("excel.Activity")
local OpenServerActConfig = ActivityExcel.openServerAct
local ActHandlerW = require("activity.HandlerW")
local InnerDataManagerFW = require("innerdata.InnerDataManagerFW")
local InnerDataDefine = require("innerdata.Define")
local ServerCommonFW = require("serverCommon.ServerCommonFW")
local Config = require("Config")

OpenServerActList = OpenServerActList or {}
CallBackList = CallBackList or {}
GMDAY = GMDAY or 0

---------------------------------
--每个活动的数据在DB中是以活动配置编号作为键进行保存的
--开服活动的数据L部分保存在DailyActFL.lua中
---------------------------------

function onActCheck()
    if true then
        return
    end

    local nTimeStamp = os.time()
    if nTimeStamp < ServerCommonFW.getFirstOpenTime() or ServerCommonFW.getFirstOpenTime() <= 0 then
        return
    end
    for nType, v in pairs(OpenServerActList) do
        if v._configList then
            for nActConfigID, nActList in pairs(v._configList) do
                local nActConfigData = OpenServerActConfig[nActConfigID]
                if not nActConfigData then
                    break
                end

                --检查是否有需要开启的开服活动
                for nActNum, nActArgs in pairs(nActList) do
                    local nStartTimeStamp = os.time(nActArgs.startDateTime)
                    local nEndTimeStamp = os.time(nActArgs.endDateTime)
                    local nLeftTime = ((nTimeStamp + GMDAY * 86400) >= nStartTimeStamp and (nTimeStamp + GMDAY * 86400) <= nEndTimeStamp) and nEndTimeStamp - (nTimeStamp + GMDAY * 86400) or 0
                    if nLeftTime > 0 then
                        if not onProcessAct(nType, nActArgs._configID, nActNum, nLeftTime, ActDefine.ACT_STATE_RUNNING, nActArgs) then
                        end
                    end
                end

                --检查进行中的活动
                if v._running then
                    local nEndTimeStamp = os.time(v._running.actArgs.endDateTime)
                    if nEndTimeStamp <= (nTimeStamp + GMDAY * 86400) then      --活动到达结束时间
                        --onEndAct(v._running.actnum)
                        onProcessAct(nType, v._running.actArgs._configID, v._running.actnum, 0, ActDefine.ACT_STATE_ENDED, v._running.actArgs)
                        v._running = nil
                    end
                end
            end
        end
    end
end

function onProcessAct(actType, actConfigID, actNum, gap, actState, actArgs)
    local actTypeData = OpenServerActList[actType]
    if not actTypeData or not actNum or not gap or not actState then
        return false
    end

    if actState == ActDefine.ACT_STATE_READY then
        if not actTypeData._configList[actConfigID][actNum] then
            actTypeData._configList[actConfigID][actNum] = actArgs
            ActHandlerW.onSend2ClientOpenServerActUpdate(actNum, 1, actArgs)
            return true
        end
    elseif actState == ActDefine.ACT_STATE_RUNNING then
        if not onCheckActIsRunning(actType) then
            actTypeData._running = {actnum = actNum, actArgs = actArgs}
            onCallBack(actType, actNum, actState, actArgs)
            onSend2LActUpdate(actType, actState, actArgs)
            --ActHandlerW.onSendBroadcast(actType, actState)
            ActHandlerW.onSend2ClientOpenServerActUpdate(actNum, 2, actArgs)
            return true
        else
            if not actTypeData._running or (actTypeData._running and actTypeData._running.actnum ~= actNum) then
                ActHandlerW.onSend2ClientOpenServerActUpdate(actNum, 2, actArgs)
            end
        end
    elseif actState == ActDefine.ACT_STATE_ENDED then
        if not actTypeData._endList[actConfigID] then
            actTypeData._endList[actConfigID] = {}
        end
        actTypeData._endList[actConfigID][actNum] = 1
        actTypeData._running = nil
        onCallBack(actType, actNum, actState, actArgs)
        ActHandlerW.onSend2ClientOpenServerActUpdate(actNum, 2, actArgs)
        onSend2LActUpdate(actType, actState, actArgs)
        --ActHandlerW.onSendBroadcast(actType, actState)
        return true
    end
    return false
end


function onGetActStatus(actType, actNum, startDateTime)
    if OpenServerActList[actType] and OpenServerActList[actType]._running then
        return ActDefine.ACT_STATE_RUNNING
    else
        local nStartTimeStamp = os.time(startDateTime)
        local nTimeStamp = os.time()
        if (nTimeStamp + (GMDAY * 86400))  < nStartTimeStamp then
            return ActDefine.ACT_STATE_READY
        else
            return ActDefine.ACT_STATE_ENDED
        end
    end
end

function onCheckIsEnd(startHour, nowHour, startMin, nowMin)
    if startHour == nowHour then
        return startMin <= nowMin
    else
        return startHour < nowHour
    end
end

function onGetLeftTime(actNum, actArgs)
    local nActConfigID = actArgs._configID
    local nActConfigData = OpenServerActConfig[nActConfigID]
    if nActConfigData then
        local nStatus = onGetActStatus(actArgs.type, actNum, actArgs.startDateTime, actArgs.endDateTime)
        if nStatus == ActDefine.ACT_STATE_READY then
            return 0
        elseif nStatus == ActDefine.ACT_STATE_RUNNING then
            local nEndTimeStamp = os.time(actArgs.endDateTime)
            local nTimeStamp = os.time()
            local nLeftTime = nEndTimeStamp - nTimeStamp - (GMDAY * 86400)
            return nLeftTime <= 0 and 0 or nLeftTime 
        else
            return 0
        end
    end
end

function onCheckActIsRunning(actType)
    if OpenServerActList[actType] and OpenServerActList[actType]._running then
        return true
    end
end

function onSend2LActUpdate(actType, status, actArgs)
    local nSend2Ldata = {
        type = 1,
        data = {
            status = status,
            actType = actType,
            actArgs = actArgs,
        }
    }
    InnerDataManagerFW.Send2LInnerData(nil, InnerDataDefine.INNERDATA_TYPE_ACT, nSend2Ldata)
end

function onCallBack(nActType, actNum, status, actArgs)
    local nCallBackData = CallBackList[nActType]
    if nCallBackData and nCallBackData[status] then
        nCallBackData[status](actNum, actArgs, nCallBackData.args)
    end
end

function onRegisterCallBack(nActType, onBeginBefore, onRunning, onEnd, actArgs)
    CallBackList[nActType] = {[ActDefine.ACT_STATE_BEFORESTART] = onBeginBefore, [ActDefine.ACT_STATE_RUNNING] = onRunning, [ActDefine.ACT_STATE_ENDED] = onEnd, args = actArgs}
end

function onDayChange(human, day1, day2)
    print("on day change")
    GMDAY = day1
    onActCheck()
    --onInit(nil, day1, day2)
end

initStatus = initStatus or false
function onInit(clear, day1, day2)
    if true then
        return
    end

    if Config.IS_MIDDLE then
        return
    end

    local nTimeStamp = os.time()
    if nTimeStamp < ServerCommonFW.getFirstOpenTime() or ServerCommonFW.getFirstOpenTime() <= 0 then
        return
    end
    if initStatus == false or clear then
        if clear then
            for nType, v in pairs(OpenServerActList) do
                for nActConfigID, nActData in pairs(v._configList) do
                    for nActNum, nActArgs in pairs(nActData) do
                        onProcessAct(nType, nActConfigID, nActNum, 0, ActDefine.ACT_STATE_ENDED, nActArgs)
                    end
                end
                OpenServerActList[nType] = nil
            end
        end

        for k, v in pairs(OpenServerActConfig) do
            if v.lasttime <= 0 then
                break
            end

            if not OpenServerActList[v.actType] then
                OpenServerActList[v.actType] = {
                    _beginbefore = nil,             --预告中的活动
                    _running = nil,                 --运行中的活动
                    _endList = {},
                }
            end

            OpenServerActList[v.actType]._configList = OpenServerActList[v.actType]._configList or {}
            local nServerOpenTime = ServerCommonFW.getFirstOpenTime()
            local nServerOpenDataTime = os.date("*t", nServerOpenTime)
            local nStartDateTime = {}
            nStartDateTime.year = nServerOpenDataTime.year 
            nStartDateTime.month = nServerOpenDataTime.month 
            nStartDateTime.day = nServerOpenDataTime.day
            nStartDateTime.hour = 0 
            nStartDateTime.min = 0 
            nStartDateTime.sec = 0 

            local nEndDateTime = {}
            local nTempStartDateTime = os.time(nStartDateTime)
            local nTempEndDateTime = os.date("*t", nTempStartDateTime + ((OpenServerActConfig[k].lasttime - 1) * 24 * 3600))
            nEndDateTime.year = nTempEndDateTime.year
            nEndDateTime.month = nTempEndDateTime.month
            nEndDateTime.day = nTempEndDateTime.day
            nEndDateTime.hour = 23
            nEndDateTime.min = 59
            nEndDateTime.sec = 59

            --if os.time((nStartDateTime.day + (day1 or 0))) <= os.time((nEndDateTime.day + (day2 or 0))) then
            --    nStartDateTime.day = nStartDateTime.day + (day1 or 0)
            --    nEndDateTime.day = nEndDateTime.day + (day2 or 0)
            --end

            local actNum = k ..nStartDateTime.year .. nStartDateTime.month .. nStartDateTime.day .. nStartDateTime.hour .. nStartDateTime.min .. nStartDateTime.sec
            if not OpenServerActList[v.actType]._configList[k] then
                OpenServerActList[v.actType]._configList[k] = {}
            end
            OpenServerActList[v.actType]._configList[k][actNum] = {_configID = k, startDateTime = nStartDateTime, endDateTime = nEndDateTime, type = v.actType}
        end
        initStatus = true
        --Util.printTable(OpenServerActList)
    end
end
